<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of OrderController
 *
 * @author Beverley
 */
class Admin_OrderController  extends Zend_Controller_Action {
    function indexAction() {
        $order = new OrderDAO();
        $orderList = $order->getOrderList();
        $paginator = Zend_Paginator::factory($orderList);
        $page = $this->_getParam('page',1);
        $paginator->setItemCountPerPage(4);
        $paginator->setCurrentPageNumber($page);
        $this->view->paginator = $paginator;
    }
    
    function updateAction() {
        if ($this->_request->isPost()) {
            $name = $this->_request->getParam('name');
            $address = $this->_request->getParam('address');
            $phone = $this->_request->getParam('phone');

            $string_validate = new Zend_Validate_StringLength(3, 15);
            if (!$string_validate->isValid($name)) {
                $mess['name'] = "the name input is too short or too long";
            }

            $string_validate = new Zend_Validate_StringLength(3, 30);
            if (!$string_validate->isValid($address)) {
                    $mess['address'] = "the address input is too short or too long";
            }
            
            $digit_validate = new Zend_Validate_Digits();
            if (!$digit_validate->isValid($phone)) {
                $mess['digit'] = "receiver phone must contain only digit character.";
            } else {
                $digit_validate = new Zend_Validate_StringLength(6, 16);
                if (!$digit_validate->isValid($phone)) {
                    $mess['greater'] = "receiver phone is too short or too long.";
                }
            }
            if (!isset($mess)) {
                $array = array(
                    'id' => $this->_request->getParam('id'),
                    'order_date' => $this->_request->getParam('order_date'),
                    'receive_date' => $this->_request->getParam('re_date'),
                    'receiver_name' => $name,
                    'receiver_address' => $address,
                    'receiver_phone' => $phone,
                    'status' => Constant::$STATUS_AVAILABLE
                );
                $order = new OrderDAO();
                $order->editOrder($array);

                $mess['sucess'] = "Update sucessfully.";
                $this->view->status = $mess;
            } else {
                $this->view->status = $mess;
            }
        }
    }

    function deleteAction() {
        $order = new OrderDAO();
        $order->deleteOrder($this->_request->getParam('id'));
        $this->_redirect('admin/order');
    }
}
?>
